<?php
namespace Modules\Box\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Card\Entities\Card;
use \Modules\Box\Entities\BoxSku;

use Validator;
use ThrowException;
use Auth;
use DB;
use Cache;

class BoxCardController extends Controller
{
    // 使用道具卡
    public function use(Request $req)
    {
        $rule = [
            'card_type' => ['required'],
            'page_uuid' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        $cardType = $req->card_type;
        $pageUuid = $req->page_uuid;
        $tipsListCacheKey = 'tips_' . $pageUuid;

        $cache = Cache::get($pageUuid) ?: ThrowException::Conflict('此页面已失效，请退出重新进入哦~');
        $skuId = $cache['sku_id'];
        $sku = BoxSku::getEntity([
            'id' => $skuId,
            'with' => ['box']
        ]);

        $box = $sku->box;

        $tipsList = @Cache::get($tipsListCacheKey) ?: [
            'exclude' => [],
            'show' => []
        ];

        // 透视卡
        if ($cardType === 'show_box') {
            $tips = '这是 ' . $sku->title;

            // 缓存使用次数
            $cachedKey = 'show_card_' . $box->uuid . $user->id . date('Y-m-d');
            $useCount = Cache::get($cachedKey) ?: 0;

            if ($box->show_card_per_day && $useCount >= $box->show_card_per_day) {
                ThrowException::Conflict('此款每人每日最高可用' . $box->show_card_per_day . '次透视卡，请明天来再哦~');
            }

            array_push($tipsList['show'], [
                'sku_id' => $sku->id,
                'title' => $sku->title
            ]);

            $user->card_asset($cardType)->out(1, [
                'description' => '使用道具卡'
            ]);

            Card::addUsedCountByType('show_box', 1);

            // 刷新缓存使用次数
            Cache::put($cachedKey, $useCount + 1, 86400);

        }
        else if($cardType === 'exclude_box') {
            $excludedSku = $sku->getBrotherBoxes(1);
            $tips = '这不是 ' . $excludedSku->title;

            // 缓存使用次数
            $cachedKey = 'exclude_box_' . $box->uuid . $user->id . date('Y-m-d');
            $useCount = Cache::get($cachedKey) ?: 0;

            if ($box->exclude_card_per_day && $useCount >= $box->exclude_card_per_day) {
                ThrowException::Conflict('此款每人每日最高可用' . $box->exclude_card_per_day . '次排除卡，请明天来再哦~');
            }

            array_push($tipsList['exclude'], [
                'sku_id' => $excludedSku->id,
                'title' => $excludedSku->title
            ]);

            $user->card_asset($cardType)->out(1, [
                'description' => '使用道具卡'
            ]);

            Card::addUsedCountByType('exclude_box', 1);

            // 刷新缓存使用次数
            Cache::put($cachedKey, $useCount + 1, 86400);
        }
        else if($cardType === 'free_tips') {
            // 免费提示
            $freeTipsCount = @Cache::get('free_tips_' . $pageUuid) ?: 0;
            $freeTipsLimit = $sku->box->free_tips_limit ?: 2;
            if ($freeTipsCount >= $freeTipsLimit) {
                $tips = '摇一摇提示不能超过' . $freeTipsLimit . '次哦~';
            }
            else {
                $freeTipsCount += 1;
                Cache::put('free_tips_' . $pageUuid, $freeTipsCount, 1800);

                $excludedSku = $sku->getExcludedSkuByFreeTips($freeTipsCount);
                $tips = '这不是 ' . $excludedSku->title;

                array_push($tipsList['exclude'], [
                    'sku_id' => $excludedSku->id,
                    'title' => $excludedSku->title
                ]);
            }
        }

        // 缓存此页的提示列表
        Cache::put($tipsListCacheKey, $tipsList, 1800);


        return [
            'tips' => $tips,
            'tips_list' => $tipsList
        ];
    }
}
